import threading
from gevent import monkey, pywsgi

monkey.patch_all()

from geventwebsocket.handler import WebSocketHandler
from flask_script import Manager

from messenger.utils.celery_utils import make_celery
from messenger import create_app

my_celery = make_celery(__name__)

app = create_app(celery=my_celery)

from messenger.utils.job_queue_util import consume_job


@app.before_first_request
def active():
    t = threading.Thread(target=consume_job, daemon=True)
    t.start()


manager = Manager(app)


@manager.command
def run_gevent():

    server = pywsgi.WSGIServer(
        (app.config.get("MESSENGER_IP"), int(app.config.get("MESSENGER_LISTEN"))),
        app,
        handler_class=WebSocketHandler,
    )
    server.serve_forever()


if __name__ == "__main__":
    manager.run()
